package com.github.browep.bat.activities;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import com.github.browep.bat.R;
import com.github.browep.bat.ScreenService;
import org.w3c.dom.Element;

import java.util.List;
import java.util.Map;
import java.util.TreeMap;

import static com.github.browep.bat.Constants.SCREEN_ID;
import static com.github.browep.bat.util.XmlUtil.*;

/**
 * Created by IntelliJ IDEA.
 * User: paul
 * Date: 8/29/11
 * Time: 11:25 AM
 * To change this template use File | Settings | File Templates.
 */
public class Menu extends BaseActivity {

    private String screenId;
    private ScreenService.Screen screen;
    private Map<String,String> priceCategories = new TreeMap<String, String> ();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setupWrappedUI(R.layout.menu);
        screenId = (String) getIntent().getExtras().get(SCREEN_ID);

        screen = getScreenService().getScreenById(screenId);

        Element menuElement = getElementByTagName(screen.getScreenElement(), "menu");
        List<Element> menuElements = getChildElementListWithTag(menuElement,"menu-item");

        List<Element> priceCategoryElements = getChildElementListWithTag(menuElement,"price-item");
        for(Element priceElement : priceCategoryElements){
            priceCategories.put(priceElement.getAttribute("id"),priceElement.getTextContent());
        }

        ListView listView = (ListView) findViewById(R.id.list);

        listView.setAdapter(new MenuListAdapter(menuElements));


    }


    public class MenuListAdapter extends BaseAdapter {

        private List<Element> elementList;

        public MenuListAdapter(List<Element> elementList) {
            this.elementList = elementList;
        }

        public int getCount() {
            return  elementList.size();
        }

        public Object getItem(int i) {
            return elementList.get(i);
        }

        public long getItemId(int i) {
            return i;
        }

        public View getView(int i, View view, ViewGroup viewGroup) {
            if( view == null){
                view = LayoutInflater.from(Menu.this).inflate(R.layout.menu_row, viewGroup, false);

                Element menuElement = (Element) getItem(i);

                String name = getChildTextContent(menuElement,"name");

                view.setTag(R.id.name,name);
                ((TextView)view.findViewById(R.id.menu_item_name)).setText(name);

                String imageName;
                ImageView imageView = (ImageView) view.findViewById(R.id.menu_item_image);
                if(hasChildElement(menuElement,"image")){
                    imageName = getChildTextContent(menuElement,"image");

                    int imageResource = getResources().getIdentifier(imageName, "drawable", getPackageName());
                    imageView.setImageResource(imageResource);
                }
                else // if there is no image to display, get rid of the view
                {
                    imageView.setVisibility(View.GONE);
                }
                ((TextView)view.findViewById(R.id.menu_item_price)).setText(buildPriceString(menuElement));


                if(hasChildElement(menuElement,"description"))
                    ((TextView)view.findViewById(R.id.menu_item_description))
                            .setText(getChildTextContent(menuElement,"description"));

            }

            return view;
        }
    }

    public String buildPriceString(Element menuElement){
        StringBuilder sb = new StringBuilder();
        for(String priceId : priceCategories.keySet()){
            sb.append(getElementByTagAndId(menuElement,"price",priceId).getTextContent().trim()).append(" ");
        }
        return sb.toString();

    }
}
